package lixp.shiro.controller;

import lixp.res.Result;
import lixp.res.ResultEnum;
import lixp.res.ResultUtil;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresRoles;
import org.apache.shiro.subject.Subject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ShiroController {

    Logger logger = LoggerFactory.getLogger("ShiroController.class");

    @PostMapping("/login")
    public Result login(String username, String password){

        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(username, password);
        try {
            subject.login(token);
        }catch (UnknownAccountException exception){
            return ResultUtil.error(ResultEnum.USER_NOT_EXIST);
        }catch (IncorrectCredentialsException exception){
            return ResultUtil.error(ResultEnum.PASSWORD_ERROR);
        }catch (LockedAccountException exception){
            return ResultUtil.error(ResultEnum.USER_LOCKED);
        }

        return ResultUtil.success();

    }

    @RequestMapping("/test")
//    @RequiresPermissions("upms:user:organization")
    @RequiresRoles("super")
    public Result test(){

         return ResultUtil.success();

    }

    @RequestMapping("/notLogin")
    public Result notLogin(){

        return ResultUtil.error(ResultEnum.NOT_LOGIN);

    }


}
